using System;
using System.Collections.Generic;
using System.Text;
using Lizk.SimpleHUD;
using SharpMedia.Controls;
using System.Data;
using Lizk.WebScraper;
using MediaData;

namespace SharpMedia.Pages
{
    public class ScraperResultPage : MediaPage
    {
        HUDImage box, icon, button;
        HUDLabel lblTitle, lblPress;
        HUDContainerSimple boxContainer;
        HUDCentering centering;
        HUDAlphaList list;
        HUDImage selection;

        MediaDataSet.TitlesRow title;
        public ScraperResultPage(HUDPage parent, MediaDataSet.TitlesRow title)
            : base(parent)
        {
            this.parent = parent;
            this.title = title;
            this.Anchor = Anchor.All;
            InitializeComponents();
        }
        private void InitializeComponents()
        {
            HUDImage background = new HUDImage("white.png");
            background.Size = Size;
            background.Color = ColorS.Black;
            background.ConstrainProportions = false;
            background.Alpha = 170;
            background.Anchor = Anchor.All;
            Add(background);


            boxContainer = new HUDContainerSimple();
            boxContainer.Size = new SizeS(500, 400);
            

            centering = new HUDCentering(this, boxContainer);
            centering.Anchor = Anchor.All;
            Add(centering);


            box = new HUDImage("box.png");
            box.ConstrainProportions = false;
            box.Size = boxContainer.Size;
            boxContainer.Add(box);

            icon = new HUDImage("web.png");
            icon.Size = new SizeS(64, 64);
            icon.Location = new PointS(20, 20);
            boxContainer.Add(icon);

            selection = new HUDImage("white.png");
            selection.Color = ColorS.Black;
            selection.Alpha = 127;
            selection.ConstrainProportions = false;
            boxContainer.Add(selection);

            list = new HUDAlphaList(new SizeS(460, 22), false);
            list.Bounds = new RectangleS(20, icon.Bounds.Bottom + 10, 460, 400 - icon.Bounds.Bottom - 20);
            boxContainer.Add(list);

            foreach (ScraperResult result in SharpMedia.ScraperHandler.Get(title).Results)
            {
                HUDLabel lbl = new HUDLabel(result.Title, "tahoma20b");
                lbl.Size = list.ItemSize;
                lbl.CharacterOffset = new PointS(0, -4);
                lbl.Tag = result;
                lbl.OnMouseEnter += new EventHandler<MouseEventArgs>(lbl_OnMouseEnter);
                lbl.OnMouseUp += new EventHandler<MouseEventArgs>(delegate { this.Command(Lizk.SimpleHUD.HUDCommands.Select, null); });
                list.Add(lbl);
            }
            
            
            UpdateSelection(false);

            lblTitle = new HUDLabel("Results for " + title["name"], "tahoma30b");
            lblTitle.Size = new SizeS(boxContainer.Size.Width - icon.Bounds.Right - 10, 64);
            lblTitle.Location = new PointS(icon.Bounds.Right + 5, icon.Bounds.Top);
            lblTitle.Alignment = Alignment.VCenter | Alignment.Left;
            boxContainer.Add(lblTitle);

            button = new HUDImage("webbutton.png");
            button.Size = new SizeS(70, 70);
            button.Location = new PointS(boxContainer.Size.Width - 100,boxContainer.Size.Height - 100);
            //boxContainer.Add(button);
            
            lblPress = new HUDLabel("Press web/ok button to Save", "tahoma20b");
            lblPress.Size = new SizeS(boxContainer.Size.Width -110, 70);
            lblPress.Location = new PointS(0, button.Bounds.Top);
            lblPress.Alignment = Alignment.VCenter | Alignment.Right;
            //boxContainer.Add(lblPress);


        }

        void lbl_OnMouseEnter(object sender, HUDControl.MouseEventArgs e)
        {
            int index = list.IndexOf((HUDControl)sender);
            if (index > -1)
            {
                list.SelectedIndex = index;
                UpdateSelection(true);
            }
        }

        public override void Show()
        {
            Fade(255, 6f);
            base.Show();
        }

        public override void Activate()
        {
            Fade(255, 6f);
            base.Activate();
        }

        public override void Close()
        {
            Fade(0, 6f);
            base.Close();
        }

        public void UpdateSelection(bool animate)
        {
            list.MoveItems(animate);
            selection.Destination = list.Location + list.Selection.Location;
            selection.DestinationSize = list.Selection.Size;
            if (!animate)
            {
                selection.Location = selection.Destination;
                selection.Size = selection.DestinationSize;
            }

        }

        public override void Command(Lizk.SimpleHUD.HUDCommands command, object tag)
        {
            switch (command)
            {
                case Lizk.SimpleHUD.HUDCommands.Up:
                    list.SelectedIndex--;
                    UpdateSelection(true);
                    break;
                case Lizk.SimpleHUD.HUDCommands.Down:
                    list.SelectedIndex++;
                    UpdateSelection(true);
                    break;
                case Lizk.SimpleHUD.HUDCommands.Back:
                    SharpMedia.Back();
                    break;
                case Lizk.SimpleHUD.HUDCommands.Left:
                    break;
                case Lizk.SimpleHUD.HUDCommands.Right:
                    break;
                case Lizk.SimpleHUD.HUDCommands.PageUp:
                    break;
                case Lizk.SimpleHUD.HUDCommands.PageDown:
                    break;
                case Lizk.SimpleHUD.HUDCommands.Web:
                case Lizk.SimpleHUD.HUDCommands.Select:
                    SharpMedia.ScraperHandler.ScrapeInfo(title, list.SelectedControl.GetTag<ScraperResult>());
                    SharpMedia.Back();
                    break;
                case Lizk.SimpleHUD.HUDCommands.Menu:
                    break;
                case Lizk.SimpleHUD.HUDCommands.Preset1:
                    break;
                case Lizk.SimpleHUD.HUDCommands.Preset2:
                    break;
                case Lizk.SimpleHUD.HUDCommands.Preset3:
                    break;
                case Lizk.SimpleHUD.HUDCommands.Play:
                    break;
                case Lizk.SimpleHUD.HUDCommands.Pause:
                    break;
                case Lizk.SimpleHUD.HUDCommands.PlayPause:
                    break;
                case Lizk.SimpleHUD.HUDCommands.Stop:
                    break;
                case Lizk.SimpleHUD.HUDCommands.Forward:
                    break;
                case Lizk.SimpleHUD.HUDCommands.Rewind:
                    break;
                case Lizk.SimpleHUD.HUDCommands.Next:
                    break;
                case Lizk.SimpleHUD.HUDCommands.Previous:
                    break;
                case Lizk.SimpleHUD.HUDCommands.KeyPress:
                    break;
                default:
                    if (parent != null)
                        parent.Command(command, tag);
                    break;
            }
        }
    }
}
